package a.b.reggie.service.impl;

import a.b.reggie.dto.DishDto;
import a.b.reggie.mapper.DishMapper;
import a.b.reggie.pojo.Dish;
import a.b.reggie.pojo.DishFlavor;
import a.b.reggie.service.IDishFlavorService;
import a.b.reggie.service.IDishService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;

@Service
@Slf4j
public class IDishServiceImpl extends ServiceImpl<DishMapper, Dish> implements IDishService {

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private IDishFlavorService dishFlavorService;

    /**
     * 新增菜品，同时添加菜品对应的口味数据
     * @param dishDto
     */
    @Transactional
    public void savaWithFlavor(DishDto dishDto) {

        //保存菜品的基本信息到菜品表dish
        this.save(dishDto);
        //获取菜品id
        Long dishId = dishDto.getId();
        //菜品口味
        List<DishFlavor> flavors = dishDto.getFlavors();
        //给每一个菜品口味赋值
        for (DishFlavor dishFlavor:flavors){
            dishFlavor.setDishId(dishId);
        }

        //保存菜品口味数据到菜品口味表dish_flavor
         dishFlavorService.saveBatch(flavors);

        //清空所有菜品的缓存
//        Set keys = redisTemplate.keys("dish_*");
//        redisTemplate.delete(keys);

        //清理某个分类下面的菜品缓存数据
        String key = "dish_" + dishDto.getCategoryId();
        redisTemplate.delete(key);

    }

    /**
     * 根据id查询菜品信息以及菜品口味
     * @param id
     * @return
     */
    @Transactional
    public DishDto getByIdWithFlavor(Long id) {
        //查询菜品基本信息
        Dish dish = this.getById(id);
        DishDto dishDto =new DishDto();
        BeanUtils.copyProperties(dish,dishDto);
        //查询菜品口味信息
        //构造条件构造器
        LambdaQueryWrapper<DishFlavor> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.eq(DishFlavor::getDishId,id);
        List<DishFlavor> flavors = dishFlavorService.list(queryWrapper);
        //将菜品口味添加到DishDto中
        dishDto.setFlavors(flavors);
        return dishDto;
    }

    /**
     * 修改菜品，同时修改口味
     * @param dishDto
     */
    @Transactional
    public void updateWithFlavor(DishDto dishDto) {
        //修改菜品基本信息
        this.updateById(dishDto);
        //清空原有口味
        LambdaQueryWrapper<DishFlavor> queryWrapper =new LambdaQueryWrapper();
        Long dishId = dishDto.getId();
        queryWrapper.eq(DishFlavor::getDishId,dishId);
        dishFlavorService.remove(queryWrapper);

        //添加修改后的口味
        List<DishFlavor> flavors = dishDto.getFlavors();
        for (DishFlavor dishFlavor:flavors){
           dishFlavor.setDishId(dishId);
        }
        dishFlavorService.saveBatch(flavors);

        //清理所有菜品的缓存数据
//        Set keys = redisTemplate.keys("dish_*");
//        redisTemplate.delete(keys);
        //清理某个分类下面的菜品缓存数据
        String key = "dish_" + dishDto.getCategoryId();
        redisTemplate.delete(key);
    }

    /**
     * 删除菜品，同时删除口味
     * @param ids
     */
    public void deleteWithFlavor(List<Long> ids) {
        //删除菜品信息
        this.removeByIds(ids);
        //删除原有口味
        LambdaQueryWrapper<DishFlavor> queryWrapper =new LambdaQueryWrapper();
        for (Long dishId:ids){
            queryWrapper.eq(DishFlavor::getDishId,dishId);
        }
        dishFlavorService.remove(queryWrapper);
    }

    /**
     * 根据分类id获取菜品
     * @param categoryId
     * @return
     */
    @Override
    public List<DishDto> userList(Long categoryId) {

        List<DishDto> dishDtoList = null;

        //动态构造key
        String key = "dish_" + categoryId;

        //先从redis中获取缓存数据
        dishDtoList = (List<DishDto>) redisTemplate.opsForValue().get(key);
        //如果存在，直接返回，无需查询数据库
        if (dishDtoList!=null){
            return dishDtoList;
        }

        //根据分类id查询菜品
        LambdaQueryWrapper<Dish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Dish::getCategoryId,categoryId);
        queryWrapper.eq(Dish::getStatus,1);
        List<Dish> dishList = this.list(queryWrapper);

        dishDtoList = new ArrayList<>();
        for (Dish dish:dishList){
            //根据菜品id查询口味
            Long dishId = dish.getId();
            LambdaQueryWrapper<DishFlavor> queryWrapper1 = new LambdaQueryWrapper<>();
            queryWrapper1.eq(DishFlavor::getDishId,dishId);
            List<DishFlavor> dishFlavorList = dishFlavorService.list(queryWrapper1);
            //将口味和菜品复制到dishDto中
            DishDto dishDto = new DishDto();
            BeanUtils.copyProperties(dish,dishDto);
            dishDto.setFlavors(dishFlavorList);
            dishDtoList.add(dishDto);
        }

        //如果不存在，需要查询数据库，将查询到的数据缓存到redis
        redisTemplate.opsForValue().set(key,dishDtoList,60, TimeUnit.MINUTES);

        return dishDtoList;
    }
}
